﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class TypeDAL
    {
        public static int Save(TypeInfo info)
        {
            string sql = string.Format("insert into [type]([Name],[Sort],[Flag],[Bak]) values('{0}',{1},'{2}','{3}');select @@identity;", info.Name, info.Sort, info.Flag, info.Bak);
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql));
        }
        public static int Update(TypeInfo info)
        {
            string sql = string.Format("update [type] set [Name]='{0}',[Sort]={1},[Bak]='{2}'  where ID={3}", info.Name, info.Sort, info.Bak, info.ID);
            return CommonDAL.ExecuteNoneQuery(sql) ? info.ID : 0;
        }
        public static TypeInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[type]");
            return Simple_Data(dr);
        }
        public static List<TypeInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[type]");
            List<TypeInfo> list = new List<TypeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<TypeInfo> FindPage(string keyword, string flag, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = " and Flag='"+flag+"'";
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[type]", out count);
            List<TypeInfo> list = new List<TypeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<TypeInfo> FindAll(string flag)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [type] where Flag='{0}'",flag));
            List<TypeInfo> list = new List<TypeInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        private static DataTable FindPage(int startindex, int pagesize, string order, string desc, string filter, out int count)
        {
            if (order == "")
            {
                order = "ID";
            }
            if (desc == "")
            {
                desc = "desc";
            }
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@startindex", startindex);
            sp[1] = new SqlParameter("@pagesize", pagesize);
            sp[2] = new SqlParameter("@order", order);
            sp[3] = new SqlParameter("@desc", desc);
            sp[4] = new SqlParameter("@filter", filter);
            sp[5] = new SqlParameter("@count", SqlDbType.Int, 10);
            sp[5].Direction = ParameterDirection.Output;
            DataTable dt = CommonDAL.GetDataTable("user_page", sp);
            count = DataConvert.ToInt(sp[5].Value);
            return dt;
        }
        private static TypeInfo Simple_Data(DataRow dr)
        {
            TypeInfo info = new TypeInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.Bak = DataConvert.ToString(dr["Bak"]);
            }
            return info;
        }
        
    }
}
